package com.qqtech.qquan.mall.dao;

import java.math.BigDecimal;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.qqtech.core.common.util.BeanUtil;
import com.qqtech.core.frame.dao.BaseDaoImpl;
import com.qqtech.qquan.mall.model.MallCart;
import com.qqtech.qquan.mall.model.MallCartQuery;
import com.qqtech.qquan.mall.model.MallCartVo;

@Repository
public class MallCartDaoImpl extends BaseDaoImpl<MallCart> implements MallCartDao {
	public static final Logger log = LoggerFactory.getLogger(MallCartDaoImpl.class);

	@Override
	public int getTotalQuantity(Integer userId, Integer shopId) {
		MallCart mallCart = new MallCart();
		mallCart.setCreatorId(userId);
		mallCart.setShopId(shopId);
		return sqlSessionTemplate.selectOne(getSqlName("totalQuantity"), mallCart);
	}

	@Override
	public BigDecimal getTotalPrice(Integer userId, Integer shopId) {
		MallCart mallCart = new MallCart();
		mallCart.setCreatorId(userId);
		mallCart.setShopId(shopId);
		return sqlSessionTemplate.selectOne(getSqlName("totalPrice"), mallCart);
	}

	@Override
	public Page<MallCartVo> queryPageBySupplier(MallCartQuery query,
			Pageable pageable) {
		List<MallCartVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectBySupplier"), getParams(query, pageable));
		return new PageImpl<MallCartVo>(contentList, pageable, this.queryCountBySupplier(query));
	}

	@Override
	public int queryCountBySupplier(MallCartQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountBySupplier"), BeanUtil.toMap(query));
	}

	@Override
	public Page<MallCartVo> queryPageByShop(MallCartQuery query,
			Pageable pageable) {
		List<MallCartVo> contentList = sqlSessionTemplate.selectList(getSqlName("selectByShop"), getParams(query, pageable));
		return new PageImpl<MallCartVo>(contentList, pageable, this.queryCountBySupplier(query));
	}

	@Override
	public int queryCountByShop(MallCartQuery query) {
		return sqlSessionTemplate.selectOne(getSqlName("selectCountByShop"), BeanUtil.toMap(query));
	}
	
}
